package luhw.copy;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;

public class Copy_finalActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        FileInputStream fis1=null;
        FileOutputStream fos1=null;
        FileInputStream fis2=null;
        FileOutputStream fos2=null;
        File file=new File("/sdcard/456");
		String[] str=file.list();
		File file2=new File("/sdcard/123");
		String[] str2=file2.list();	
		List<String> list1=new ArrayList<String>();
		List<String> list2=new ArrayList<String>();
		for(int i=0;i<str2.length;i++){
			list1.add(str2[i]);		
		}
		for(int j=0;j<str.length;j++){
			list2.add(str[j]);
		}
		for(int m=0;m<list2.size();m++){
			for(int n=0;n<list1.size();n++){
				if(list2.get(m).equals(list1.get(n))){
					for(String s:str){
						int a=0;
						byte[] buff=new byte[1024];
						try {
							fis1=new FileInputStream("/sdcard/456/"+s+"");
							//File file3=new File("/sdcard/789/"+s+"");
							//if(!file3.exists()){
								//file3.createNewFile();
							//}
							fos1=new FileOutputStream("/sdcard/789/"+s+"");
						} catch (FileNotFoundException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (IOException e) {
							// TODO Auto-generated catch block
							System.out.println(e.getMessage());
							e.printStackTrace();
						}
						
						try {
							while((a=fis1.read(buff))!=-1){
								try {
									fos1.write(buff, 0, a);
								} catch (IOException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
								
							}
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}finally{
							try {
								fis1.close();
								fos1.close();
							} catch (IOException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
							
						}
					}
					list1.remove(n);
				}
				
			}
		}
		for(int a=0;a<list1.size();a++){
			int b=0;
			byte[] buff=new byte[1024];	
			try {
				fis2=new FileInputStream("/sdcard/123/"+list1.get(a)+"");
				fos2=new FileOutputStream("/sdcard/789/"+list1.get(a)+"");
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				while((b=fis2.read(buff))!=-1){
					fos2.write(buff, 0, b);
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally{
				try {
					fis2.close();
					fos2.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}			
			}
		}
    }
}